package cn.yunhe;

import cn.yunhe.model.*;
import cn.yunhe.util.MapUtil;
import cn.yunhe.util.TransverseMapUtil;

import javax.swing.*;
import java.util.List;
import java.util.Random;

/**
 * 程序入口
 */
public class YangLeGeYangApp extends JFrame {
    public static final Map map = MapUtil.buildMap(6);
    public static final TransverseMap transverseLayer = TransverseMapUtil.buildTransverseMap(18);
    private Brand bj = new Brand("背景草地");//背景图⽚
    private Brand xc = new Brand("消除区域");//消除背景图⽚
    

    public YangLeGeYangApp() {
        //1.初始化基本信息
        init();

        //绘制背景和消除区域
        xc.setBounds(0,575,450,800);
        this.getContentPane().add(xc);
        bj.setBounds(0,0,450,800);
        this.getContentPane().add(bj);
//        transverseLayer.setBounds(60,500,50,50);
//        this.getContentPane().add(transverseLayer);

        //2.渲染图层：添加组件的⽅法，可以添加⾃⼰定义的组件都当前的窗⼝中
        List<Layer> layerList = map.getList();
        for (int i = 0; i <layerList.size(); i++) {
            renderLayer(layerList.get(i));
        }

        List<Layer> transverseLayerList = transverseLayer.getList();
        for (int i = 0; i <transverseLayerList.size(); i++) {
            renderTransverseLayer(transverseLayerList.get(i));
        }
        
        //3.调⽤compareAll⽅法，判定是否为正常图⽚
        map.compareAll();
        transverseLayer.compareAll();
        //4.⾃动刷新
        autoRefresh();
        //5.调⽤播放⾳乐的⽅法
        new Music().music();
        
    }
    /**
     * 渲染的图层的数据
     */
    private void renderLayer(Layer layer) {
        //获取单元格
        Cell[][] cells = layer.getCells();
        for (int row = 0; row < cells.length; row++) {
            for (int col = 0; col < cells[row].length; col++) {
                Brand brand = cells[row][col].getBrand();
                int x = col * 50 + layer.getOffsetx();
                int y = row * 50 + layer.getOffsety();//加⼊y轴的偏移量
                brand.setBounds(x+30,y,50,50);
                //注意改为LayeredPane否则出现遮盖
                this.getLayeredPane().add(brand);
                System.out.print(brand.getName()+"-");
            }
            System.out.println();
        }
    }

    private void renderTransverseLayer(Layer layer) {
        //获取单元格
        Cell[][] cells = layer.getCells();
        for (int row = 0; row < cells.length; row++) {
            for (int col = 0; col < cells[row].length; col++) {
                Brand brand = cells[row][col].getBrand();
                int x = 110 + 10 * col;
                brand.setBounds(x,500,50,50);
                //注意改为LayeredPane否则出现遮盖
                this.getLayeredPane().add(brand);
                System.out.print(brand.getName()+"-");
            }
            System.out.println();
        }
    }

    

    //⾃动刷新线程
    private void autoRefresh() {
        YangLeGeYangApp start = this;
        new Thread(() -> {
            while (true) {
                start.repaint();
                int count = 0;
                for (Layer layer : YangLeGeYangApp.map.getList()) {
                    for (Cell[] cell : layer.getCells()) {
                        for (Cell cell1 : cell) {
                            if (cell1.getState() == 1) {
                                count++;
                            }
                        }
                    }
                }
                if (count == 0) {
                    System.out.println("游戏结束");
                    JDialog jDialog = new JDialog();
                    //创建一个管理图片的容器对象
                    JLabel jLabel = new JLabel(new ImageIcon("imgs/111.png"));
                    jLabel.setText("恭喜你，成功通关！");
                    jLabel.setVerticalTextPosition(JLabel.BOTTOM);
                    jLabel.setBounds(0, 0, 258, 258);
                    jDialog.getContentPane().add(jLabel);
                    jDialog.setSize(344, 344);
                    //让弹框置顶
                    jDialog.setAlwaysOnTop(true);
                    jDialog.setLocationRelativeTo(null);    //设置位置居中
                    //弹框不关闭，则不能操作
                    jDialog.setModal(true);
                    jDialog.setVisible(true);
                    break;
                }
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
    private void init(){
        this.setTitle("⽺了个⽺");
        this.setSize(450,800);
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗体，推出程序
        this.setVisible(true);  //设置此窗体是否可由用户调整大小。
        this.setLayout(null);//来设置用户界面上的屏幕组件的格式布局，默认为流式布局
    }
    public static void main(String[] args) {
        new YangLeGeYangApp();
    }
}